extends Node2D

var settingsInfo: SettingsInfo = null

func _ready():
	visible = false
	settingsInfo = DataStorageManager.load_settingsInfo()
	if settingsInfo == null:
		settingsInfo = SettingsInfo.new()
	if settingsInfo.is_fullScreen:
		_on_全屏_pressed()
	else:
		_on_退出全屏_pressed()
	$"ColorRect/框/背景音量/HSlider".value = settingsInfo.volume_music
	$"ColorRect/框/音效音量/HSlider".value = settingsInfo.volume_effect

# 认输
func _on_surrender_pressed():
	ObjectManager.soundManager.play(SoundType.button_click)
	#判读当时是否在对局中，不在游戏中不能认输
	if ObjectManager.is_inGame:
		if !ObjectManager.gameManager.me_player:
			ObjectManager.toast.showToast("观战不能认输")
		elif ObjectManager.gameManager.me_player.alive:
			WebSocketManager.sendObj(false,"admitDefeat",{})
		else :
			ObjectManager.toast.showToast("死人不能认输")
		visible = false
	else:
		ObjectManager.toast.showToast("游戏中才可以认输")

# 关闭设置
func _on_back_pressed():
	ObjectManager.soundManager.play(SoundType.button_click)
	visible = false

# 退出游戏
func _on_退出游戏_pressed():
	ObjectManager.soundManager.play(SoundType.button_click)
	get_tree().quit()


func _on_全屏_pressed():
	DisplayServer.window_set_mode(DisplayServer.WINDOW_MODE_FULLSCREEN)
	$"ColorRect/框/退出全屏".visible = true
	$"ColorRect/框/全屏".visible = false
	settingsInfo.is_fullScreen = true
	DataStorageManager.save_settingsInfo(settingsInfo)

func _on_退出全屏_pressed():
	DisplayServer.window_set_mode(DisplayServer.WINDOW_MODE_WINDOWED)
	$"ColorRect/框/退出全屏".visible = false
	$"ColorRect/框/全屏".visible = true
	settingsInfo.is_fullScreen = false
	DataStorageManager.save_settingsInfo(settingsInfo)

# 点击设置外空白区域
func _on_color_rect_gui_input(event):
	if event is InputEventMouseButton:
		if event.button_index == MOUSE_BUTTON_LEFT and event.pressed:
			ObjectManager.soundManager.play(SoundType.button_click)
			visible = false

# 背景音量
func _on_背景音量条_value_changed(value):
	if value == -50:
		AudioServer.set_bus_mute(2, true)
	else:
		AudioServer.set_bus_mute(2, false)
	AudioServer.set_bus_volume_db(2, value)
	settingsInfo.volume_music = value
	DataStorageManager.save_settingsInfo(settingsInfo)

# 音效音量
func _on_音效音量条_value_changed(value):
	if value == -50:
		AudioServer.set_bus_mute(1, true)
	else:
		AudioServer.set_bus_mute(1, false)
	AudioServer.set_bus_volume_db(1, value)
	settingsInfo.volume_effect = value
	DataStorageManager.save_settingsInfo(settingsInfo)
